<?php
 require "../../document/class_corpus.php";
 require "../../document/class_document.php";
 require "../../document/class_document_xml.php";

 require "../../files/tool_files_dirs.php";
 require "../../misc/class_tag.php";

 require "../../tree/class_tree.php";
 require "../../tree/class_tree_xhtml.php";
 require "../../tree/class_structure.php";
 require "../../tree/tool_tree.php";

 require "../../array/tool_array.php";

 require "../../string/tool_string.php";
 require "../../string/class_segment.php";

 require "../../pdo/class_sql_document.php";

$exp = new sql_experience("align_ecr_selected");
$array_obj_sql = $exp->reader_experience();

$data_lg = $array_obj_sql['sql_document']->reader_all_lg();

$start = microtime(true);
$global_array_corres = array();
$res_all_count_bloc = array();
foreach($data_lg as $info){
  $lg = $info['lg'];
  $array_corres = $array_obj_sql["sql_rstr"]->reader_all2relative_id_bloc($lg);
  $global_array_corres[] = $array_corres;
  $array_obj_sql["sql_word"]->reader_all2count_bloc2($res_all_count_bloc,$lg,$array_corres);
}
$end=microtime(true)-$start;
echo "\n total1 :: $end\n";


foreach($res_all_count_bloc as $rep=>$array_str){
  $array = array();
  foreach($array_str as $str_info){
    $array[$str_info[1]][] = array($str_info[0],$str_info[2],$str_info[3]);
  }
  $res_all_count_bloc[$rep] = $array;
}

//print_r($res_all_count_bloc);
//die();

foreach($res_all_count_bloc as $rep=>$array_lg){
  if(count($array_lg) == count($data_lg)){
    $array_rep = explode("|",$rep);
    if(count($array_rep)>1){
      echo $rep."\n";
      foreach($array_lg as $lg=>$array_info){
//      if($lg == "fr"){
          echo "  $lg\n";
          foreach($array_info as $i){
            $type = $array_obj_sql["sql_chunk"]->reader_id_word2info_type($i[2]);
            echo " $i[1] ";
          }
          echo "\n";
//      }
      }
    }
  }
}
die();
print_r($res_all_count_bloc);

die();



$corpus_multidoc = array();

$id_chunk = 1;

$array_factory = array();


$doc2bloc = array();
$id_bloc2text = array();

//$array_word = $array_obj_sql['sql_chunk']->reader_id_bloc2info_chunk(2267);
//print_r($array_word);
//die();

foreach($corpus_multidoc as $id_multidoc=>$multidoc){
  foreach($multidoc as $lg=>$id_doc){
    $r = $array_obj_sql['sql_bloc']->reader_id_document2array($id_doc);
    foreach($r as $info_bloc){
      $id_bloc = $info_bloc['id_bloc'];
      $texte = $info_bloc['contenu_text'];
      $doc2bloc[$id_multidoc][$lg][$id_doc][] = $id_bloc;
    }
/*
    foreach($r as $info_bloc){
      $id_bloc = $info_bloc['id_bloc'];
      $id_bloc2text[$id_bloc] = $info_bloc['contenu_text'];
      $array_word = $array_obj_sql['sql_chunk']->reader_id_bloc2info_chunk($id_bloc);
      $bloc2chunk[$lg][$id_doc][$id_bloc] = $array_word;
    }
*/
  }
}

print_r($doc2bloc);

die();


$id_multidoc = 1;
$multidoc = $corpus_multidoc[$id_multidoc];
$lg_src = 'fr';
$lg_tgt = 'es';
$id_doc_src = $multidoc[$lg_src];
$id_doc_tgt = $multidoc[$lg_tgt];

$bc_src = $bloc2chunk[$lg_src][$id_doc_src];
$bc_tgt = $bloc2chunk[$lg_tgt][$id_doc_tgt];

function bc2ac($v,&$txt){
  $ac = array();
  foreach($v as $id_chunk => $array_mot){
    $am = array();
    foreach($array_mot as $i){
      $am[] = new mot($txt, $i[1], $i[2], array("type"=>$i[4]));
    }
    $ac[] = new chunk($am);
  }
  return $ac;
}

function chunk2rstr($ac,$id_bloc,&$array_obj_sql){
  $res = array();
  foreach($ac as $id=>$c_src){
    $o_src = $c_src->__get_offset();
    $l_src = $c_src->__get_len();
    $candidats = $array_obj_sql['sql_rstr']->reader_seg2seg_inside($id_bloc,$o_src,$l_src);
    $inside = $union = $outside = array();
    foreach($candidats as $cand){
      (is_inside($o_src,$l_src,$c['offset'],$c['len'])) ? $inside[]=$cand : $outside[]=$cand;
    }
    $res[$id] = array('outside' => $outside, 'inside' => $inside);
  }
  return $res;
}

$time_start = microtime(true);
$cpt = 0;
while(list($k1,$v1) = each($bc_src) and list($k2,$v2) = each($bc_tgt)){
  $ac_src = bc2ac($v1,$id_bloc2text[$k1]);
  $ac_tgt = bc2ac($v2,$id_bloc2text[$k2]);
  if($cpt == 1){
    foreach($ac_tgt as $c){
      print $id_bloc2text[$k2];
      print "\n";
      print $c;
      print "\n";
    }
  }
//  $chunk = reset($ac_src);
//  $g = $chunk->__get_str();
  $rstr_src = chunk2rstr($ac_src,$k1,$array_obj_sql);
  $rstr_tgt = chunk2rstr($ac_tgt,$k2,$array_obj_sql);

/*
  foreach($rstr_src as $id_chunk=>$info){
      $chunk = $ac_src[$id_chunk];
      $g = "$chunk";
      $o = $chunk->__get_offset();
      $l = $chunk->__get_len();
      echo "inside\n";
      print_inside($g,$o,$l,$info['inside']);
      echo "outside\n";
      print_outside($g,$o,$l,$info['outside']);
      die();
  }
*/
  if($cpt == 1) die();
  ++$cpt;
}
$time_end = microtime(true);
$time=$time_end-$time_start;
echo "time=$time\n";
  die();


foreach($bloc2chunk[$lg_src][$id_doc_src] as $id_bloc=>$array_chunk){
  echo "id_bloc::$id_bloc\n";
  foreach($array_chunk as $id_chunk => $array_mot){
    $nc = new chunk();
    echo " [";
    foreach($array_mot as $mot){
      echo $mot[3]." ";
    }
    echo "] ";
  }
  echo "\n\n";
}
die();

foreach($array_factory as $lg=>$factory){
  $array_data = array();
  print "$lg::e\n";
  $factory->init_etiquetage();
  print "$lg::c\n";
  $factory->init_chunk();
  print "$lg::get\n";
  $rep = $factory->__get_repartition_chunk();
  print "$lg::svg\n";
  foreach($rep as $id_doc=>$array_doc){
    foreach($array_doc as $id_bloc=>$array_bloc){
      foreach($array_bloc as $chunk){
        foreach($chunk as $id_word){
          $word = $factory->__get_segment($id_word);
          $id_word = $word->__get_id_word();
          $array_data[]=array(
            'id_chunk'=>$id_chunk,
            'id_word'=>$id_word
          );
        }
        ++$id_chunk;
      }
    }
  }
  $array_obj_sql['sql_chunk']->writer_array_values($array_data);
  unset($array_data);

}

print $id_chunk;
//  tool_factory_mot::factory2print($factory_mot);
  die();
  print_r($r);
  die();

print_r($doc2bloc["fr"]);

die();

$data=$array_obj_sql['sql_bloc']->reader_all2exec_plus_lg();
$factory_mot=new factory_mot();
foreach($data as $ligne){
  print_r($ligne);
  die();
  $factory_mot->get_all_mots($ligne['contenu_text']);
}


die();

$a1 = array('le','petit','chat');

$factory_mot=new factory_mot();

foreach($a1 as $w){
  $mot = new mot();
  $factory_mot->__add_word();
}


$factory_mot->get_all_mots($a[0]);
$factory_mot->get_all_mots($a[1]);
$factory_mot->get_all_mots($a[2]);

$factory_mot->init_etiquetage();
$factory_mot->init_chunk();
$time_end = microtime(true);
$time=$time_end-$time_start;
echo "time=$time\n";

tool_factory_mot::factory2print($factory_mot);

die();



 $corpus=tool_corpus::get_corpus_regexp("../../../Corpora/europa_complete_repair/","4.fr.xml");
// $corpus=tool_corpus::get_corpus_regexp("../xml/test/",".fr.xml");
 $array_doc=$corpus->__get_array_doc();
 $document=$array_doc[0];
// print_r($document);
// die();
 $structure=$document->__get_structure();
 $signature=$structure->__get_signature();
 tool_tree::cleanstructure($signature);
 $signature->__recalc_idn();

 //echo tool_tree::structure2texte($signature);
 $test=new RecursiveIteratorIterator($signature,RecursiveIteratorIterator::SELF_FIRST);

 $factory_mot=new factory_mot();

 $time_start = microtime(true);
 foreach($test as $bloc_xhtml){
    $texte=tool_tree::tree_xhtml2texte($bloc_xhtml->__get_value());
    $id=$factory_mot->get_all_mots($texte);
//    $factory_mot->get_array_words($texte);
//    echo "\r$id";
   //print_r(tool_tree::tree_xhtml2texte($bloc_xhtml));
   //echo $bloc_xhtml;
 }

$factory_mot->init_etiquetage();
$factory_mot->init_chunk();
$time_end = microtime(true);
$time=$time_end-$time_start;
echo "time=$time\n";

tool_factory_mot::factory2print($factory_mot);

//tool_factory_mot::factory2dictionnairetexte($factory_mot);
// tool_factory_mot::factory2texte($factory_mot); 
// echo tool_tree::structure2texte($signature);
// print_r(count($array_doc));
die();

foreach($array_doc as $doc){
 $structure=$doc->__get_structure();
 $signature=$structure->__get_signature();
 //echo get_class($structure);
 //print_r($signature);
 echo tool_tree::structure2texte($signature); 
}



die();
 $array_ids_europa=tool_corpus::europa_get_all_ids("../xml/europa/");
 $array_corpus_groupe=array();

 $time_start = microtime(true);
 foreach($array_ids_europa as $id_europa){
  $corpus=tool_corpus::get_corpus_regexp("../xml/europa/",$id_europa);
  $array_corpus_groupe[$id_europa]=array();
  //echo "$id_europa\n";
  foreach($corpus->__get_array_doc() as $document){
    $structure=$document->__get_structure();
    $signature=$structure->__get_signature();
    tool_tree::cleanstructure($signature);
    $signature->__recalc_idn();
    echo tool_tree::structure2texte($signature);
  }
  die();
 }
 die();


?>
